import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'package:url_launcher/url_launcher.dart';

// 定义常量颜色
const Color kLinkColor = Colors.blue;

// 封装包含链接的 TextSpan 组件
TextSpan buildLinkTextSpan(BuildContext context) {
  return TextSpan(
    children: [
      const TextSpan(text: "Home: "),
      TextSpan(
        text: "https://flutterchina.club",
        style: TextStyle(color: kLinkColor),
        recognizer:
            TapGestureRecognizer()
              ..onTap = () async {
                try {
                  final uri = Uri.parse("https://flutterchina.club");
                  // 将 onTap 函数标记为  以支持 await 关键字
                  if (await canLaunchUrl(uri)) {
                    await launchUrl(uri);
                  } else {
                    throw 'Could not launch $uri';
                  }
                } catch (e) {
                  ScaffoldMessenger.of(
                    context,
                  ).showSnackBar(SnackBar(content: Text('打开链接失败: $e')));
                }
              },
      ),
    ],
  );
}

// 文本展示页面
class TextPage extends StatelessWidget {
  const TextPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('文本组件展示')),
      body: Center(
        child: ListView(
          padding: EdgeInsets.all(16),
          children: [
            // 链接文本
            _buildLinkCard(context),

            // 文本样式对比
            _buildStyleDemoCard(context),

            // 多层级TextSpan
            _buildTextSpanDemoCard(context),

            // DefaultTextStyle继承
            _buildDefaultTextStyleDemo(context),

            // 字体展示
            _buildFontDemoCard(context),
          ],
        ),
      ),
    );
  }
}

// 新增的演示组件
Card _buildLinkCard(BuildContext context) {
  // 原有链接演示
  return Card(
    child: Padding(
      padding: EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('链接演示', style: Theme.of(context).textTheme.titleLarge),
          Text.rich(buildLinkTextSpan(context)),
        ],
      ),
    ),
  );
}

// 新增的演示组件
Card _buildStyleDemoCard(BuildContext context) {
  return Card(
    child: Padding(
      padding: EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('文本样式对比', style: Theme.of(context).textTheme.titleLarge),
          Text('常规文本', style: TextStyle(fontSize: 16)),
          Text('加粗文本', style: TextStyle(fontWeight: FontWeight.bold)),
          Text('斜体文本', style: TextStyle(fontStyle: FontStyle.italic)),
          Text('带下划线', style: TextStyle(decoration: TextDecoration.underline)),
          Text('红色大字号', style: TextStyle(color: Colors.red, fontSize: 20)),
        ],
      ),
    ),
  );
}

Card _buildTextSpanDemoCard(BuildContext context) {
  return Card(
    child: Padding(
      padding: EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('TextSpan嵌套演示', style: Theme.of(context).textTheme.titleLarge),
          Text.rich(
            TextSpan(
              children: [
                TextSpan(text: '基础文本 '),
                TextSpan(
                  text: '蓝色可点击',
                  style: TextStyle(
                    color: Colors.blue,
                    fontSize: 18,
                    decoration: TextDecoration.underline,
                  ),
                  recognizer:
                      TapGestureRecognizer()
                        ..onTap = () {
                          ScaffoldMessenger.of(
                            context,
                          ).showSnackBar(SnackBar(content: Text('点击了蓝色文本')));
                        },
                ),
                TextSpan(
                  text: ' 带背景色',
                  style: TextStyle(
                    backgroundColor: Colors.amber[100],
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    ),
  );
}

Card _buildDefaultTextStyleDemo(BuildContext context) {
  return Card(
    child: DefaultTextStyle(
      style: TextStyle(color: Colors.red, fontSize: 16, height: 1.5),
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'DefaultTextStyle继承演示',
              style: Theme.of(
                context,
              ).textTheme.titleLarge?.copyWith(color: Colors.black),
            ),
            Text('继承红色文字'),
            Text('继承行高和字号', style: TextStyle(color: Colors.green)),
            Text(
              '混合样式',
              style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    ),
  );
}

Card _buildFontDemoCard(BuildContext context) {
  return Card(
    child: Padding(
      padding: EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('字体展示', style: Theme.of(context).textTheme.titleLarge),
          Text(
            '系统默认字体 - Roboto',
            style: TextStyle(fontSize: 16, fontFamily: 'Roboto'),
          ),
          Text(
            '自定义字体 - Poppins',
            style: TextStyle(fontSize: 16, fontFamily: 'Poppins'),
          ),
          Text(
            'Material Icons',
            style: TextStyle(
              fontFamily: 'MaterialIcons',
              fontSize: 16,
              color: Colors.purple,
            ),
          ),
        ],
      ),
    ),
  );
}
